package com.jerehedu.myapplication34.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.jerehedu.myapplication34.R;
import com.jerehedu.myapplication34.adapter.ListViewAdapter;
import com.jerehedu.myapplication34.dao.BookDao;
import com.jerehedu.myapplication34.entrty.Book;
import com.jerehedu.myapplication34.util.GetAllBookFromPhone;
import com.jerehedu.myapplication34.util.ThreadPoolManager;

import java.util.ArrayList;

public class LocalLeadToActivity extends AppCompatActivity {
    private ListView listView;
    private ArrayList<Book> mData=new ArrayList<>();
    private ListViewAdapter adapter;
    private Button chooseAll,add,delete;
    private BookDao dao;
    private ImageView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_local_lead_to);
        dao=new BookDao(LocalLeadToActivity.this);
        listView=(ListView)findViewById(R.id.listView);
        chooseAll=(Button)findViewById(R.id.chooseAll);
        back=(ImageView)findViewById(R.id.back);
        add=(Button)findViewById(R.id.add);
        delete=(Button)findViewById(R.id.delete);
        adapter=new ListViewAdapter(LocalLeadToActivity.this,mData);
        listView.setAdapter(adapter);
        ThreadPoolManager.getInstance().getExecutorService().execute(new Runnable() {
            @Override
            public void run() {
                GetAllBookFromPhone getAllBookFromPhone = new GetAllBookFromPhone();
                ArrayList<Book> list = getAllBookFromPhone.findAllName(LocalLeadToActivity.this);
                Message msg = handler.obtainMessage(1);
                msg.obj = list;
                handler.sendMessage(msg);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        chooseAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (chooseAll.getText().equals("全选")) {
                    for (Book book : mData) {
                        book.setIsCheck(1);
                    }
                    chooseAll.setText("全不选");
                } else {
                    for (Book book : mData) {
                        book.setIsCheck(0);
                    }
                    chooseAll.setText("全选");
                }
                adapter.notifyDataSetChanged();
            }
        });

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog=new AlertDialog.Builder(LocalLeadToActivity.this);
                View view= LayoutInflater.from(LocalLeadToActivity.this).inflate(R.layout.addbook_dialog_item,null);
                alertDialog.setView(view);
                final Dialog dialog=alertDialog.show();
                Button yes=(Button)view.findViewById(R.id.yes);
                yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ArrayList<Book> list=new ArrayList<Book>();
                        for(Book book:mData){
                        if(book.getIsCheck()==1){
                            list.add(book);
                        }
                    }
                        dao.addBooks(list);
                        Log.e("hhh", dao.findAllBook().size() + "");
                        for (Book book : mData) {
                            book.setIsCheck(0);
                        }
                        //跳转到MainActivity中
                        finish();
                    }
                });
                Button no=(Button)view.findViewById(R.id.no);
                no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        for (Book book : mData) {
                            book.setIsCheck(0);
                        }
                        adapter.notifyDataSetChanged();
                        if (chooseAll.getText().equals("全不选")) {
                            chooseAll.setText("全选");
                        }
                    }
                });

            }
        });

        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder alertDialog=new AlertDialog.Builder(LocalLeadToActivity.this);
                View view= LayoutInflater.from(LocalLeadToActivity.this).inflate(R.layout.deletebook_dialog_item,null);
                alertDialog.setView(view);
                final Dialog dialog=alertDialog.show();
                Button yes=(Button)view.findViewById(R.id.yes);
                yes.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ArrayList<Book> list = new ArrayList<Book>();
                        for (Book book : mData) {
                            if (book.getIsCheck() == 1) {
                                list.add(book);
                            }
                        }
                        dao.removeBooks(list);
                        mData.removeAll(list);
                        adapter.notifyDataSetChanged();
                        dialog.dismiss();
                        if(chooseAll.getText().equals("全不选")){
                            chooseAll.setText("全选");
                        }
                        Log.e("hhh1", dao.findAllBook().size() + "");
                    }
                });
                Button no=(Button)view.findViewById(R.id.no);
                no.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        for (Book book : mData) {
                           book.setIsCheck(0);
                        }
                        adapter.notifyDataSetChanged();
                        if(chooseAll.getText().equals("全不选")){
                            chooseAll.setText("全选");
                        }
                    }
                });
            }
        });




    }

    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what==1){
                ArrayList<Book> books=(ArrayList<Book>)msg.obj;
                mData.addAll(books);
                adapter.notifyDataSetChanged();
            }
        }
    };
}
